package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.collect.CollectBo;
import com.cskaoyan.bean.collect.CollectListVo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.fasterxml.jackson.databind.type.CollectionLikeType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Austin Hsu
 * @Date 2022/9/12 11:04
 */
@Service
public class CollectionServiceImply implements CollectionService {
    @Autowired
    MarketCollectMapper marketCollectMapper;


    /**
     * 客户端 收藏和删除收藏 发的请求体是同一个 ---> {"type":0,"valueId":1127003}
     * 1.先根据user_id和value_id   select 表market_collect中是否有这条数据
     * 2.如果有这条数据 判断这条数据在表中的deleted属性为0还是1  是0则update delete=1 是1则update deleted=0
     * 如果没有这条数据 insert这条数据
     */
    @Override
    public void addordelete(CollectBo collectBo) {
        //得到User信息
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer uid = marketUser.getId();

        collectBo.setUserId(uid);
        collectBo.setUpdateTime(new Date());
        collectBo.setAddTime(new Date());

        int type = collectBo.getType();
        int valueId = collectBo.getValueId();
        Boolean flag = marketCollectMapper.queryIdIfExist(type, valueId, uid);   //返回一个Boolean类型的deleted吧 没有的话就是null 有的话就是0 收藏过但取消了就是1
        if (flag == null) {    //没有这条收藏 插入
            marketCollectMapper.insertCollect(collectBo);
        }else if (flag == false) {        //收藏过 取消收藏     ---这里不用else if的话会空指针异常 null==false
            marketCollectMapper.deleteCollect(collectBo);
        }

    }


/*    @Override
    public void addordelete(CollectBo collectBo) {
        //得到User信息
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer uid = marketUser.getId();

        collectBo.setUserId(uid);
        collectBo.setUpdateTime(new Date());
        collectBo.setAddTime(new Date());

        int type = collectBo.getType();
        int valueId = collectBo.getValueId();
        Boolean flag = marketCollectMapper.queryIdIfExist(type, valueId, uid);   //返回一个Boolean类型的deleted吧 没有的话就是null 有的话就是0 收藏过但取消了就是1
        if (flag == null) {    //没有这条收藏 插入
            marketCollectMapper.insertCollect(collectBo);
        }else if (flag == false) {        //收藏过 取消收藏     ---这里不用else if的话会空指针异常 null==false
            marketCollectMapper.updateCancelCollect(collectBo);
        }else if (flag == true) {         //取消收藏过 进行收藏操作
            marketCollectMapper.updateCollect(collectBo);
        }

    }*/          //逻辑删除 前台没有实现 （deleted=1或者0）

    @Override
    public CommonData<CollectListVo> query(String type, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        //得到User信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer uid = marketUser.getId();

        List<CollectListVo> list = marketCollectMapper.query(uid);     //出问题的话可以把这里的uid改成1测试
        PageInfo<CollectListVo> pageInfo = new PageInfo<>(list);
        int total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();

        CommonData<CollectListVo> data = new CommonData<>(total, pages, info.getLimit(), info.getPage(), list);

        return data;
    }
}
